import "dotenv/config";
import inquirer from "inquirer";

import { questions } from "./questions/article";
import { getJuejin } from "./utils/getArticle";
import { downArticle } from "./utils/getJuejin";
import { Article } from "./types/article";
import { writeFileByType } from "./utils/util";

inquirer.prompt(questions).then(async (answers) => {
  console.log(answers);
  if (answers.website === "掘金(https://juejin.im/)") {
    const res = await getJuejin();
    if (res) {
      const body = JSON.parse(res.body);
      const articles: Article[] = body.data
        .filter((m: any) => m.item_type === 2)
        .map((m: any) => ({
          id: m.item_info.article_id,
          title: m.item_info.article_info.title,
          brief_content: m.item_info.article_info.brief_content,
          username: m.item_info.author_user_info.user_name
        }));
      writeFileByType("juejin.article.list", articles, 'json');
      await Promise.all(articles.map((m) => downArticle(m)));
    }
  }
});
